package com.hxkcloud.patient.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hxkcloud.patient.R;
import com.hxkcloud.patient.beans.DpatientInfoWholeBean;
import com.hxkcloud.patient.beans.MonitorKindBean;
import com.hxkcloud.patient.dborm.MonitorKindDao;
import com.hxkcloud.patient.request.MessageCode;
import com.hxkcloud.patient.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.patient.utils.ImageUtil;
import com.hxkcloud.patient.views.FlowLayout;

import java.io.Serializable;

/**
 * Created by william_sun on 2016-10-11.
 * Description:医生端的患者信息页
 */
public class DpatientInfoActivity extends BaseActivity implements View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private SwipeRefreshLayout swipeRefreshLayout;

    private TextView tv_name;
    private TextView tv_sex;
    private TextView tv_age;

    //健康监测
    private LinearLayout ll_jkjc;
    private FlowLayout flow_jkjc;
    //用药记录
    private LinearLayout ll_yyjl;
    private FlowLayout flow_yyjl;
    //检查报告
    private LinearLayout ll_jcbg;
    private FlowLayout flow_jcbg;
    //专科评估
    private LinearLayout ll_zkpg;
    private FlowLayout flow_zkpg;
    //调查问卷
    private LinearLayout ll_dcwj;
    private FlowLayout flow_dcwj;
    //患者信息
    private LinearLayout ll_hzxx;
    private FlowLayout flow_hzxx;
    //康复评价
    private TextView tv_evalution;

    private String patientId;
    private String patientName = "";
    private String patientAge = "";
    private String patientSex = "";

    private DpatientInfoWholeBean infoBean;

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_PATIENTUSEDLIST_SUCCESS:
                swipeRefreshLayout.setRefreshing(false);
                infoBean = (DpatientInfoWholeBean) msg.obj;
                showData();
                break;
            case MessageCode.NETWORKCODE_PATIENTUSEDLIST_FAILURE:
                swipeRefreshLayout.setRefreshing(false);
                if (msg.obj != null && !TextUtils.isEmpty((String) msg.obj)) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dpatientinfo);

        Intent iGet = getIntent();
        patientId = iGet.getStringExtra("patientid");
        patientName = iGet.getStringExtra("name");
        patientAge = iGet.getStringExtra("age");
        patientSex = iGet.getStringExtra("sex");

        initView();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(patientName);
        iv_back.setOnClickListener(this);

        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swiperefresh_dpatientinfo);

        tv_name = (TextView) findViewById(R.id.tv_dpatientinfo_name);
        tv_age = (TextView) findViewById(R.id.tv_dpatientinfo_age);
        tv_sex = (TextView) findViewById(R.id.tv_dpatientinfo_sex);
        ll_jkjc = (LinearLayout) findViewById(R.id.ll_dpatientinfo_jkjc);
        flow_jkjc = (FlowLayout) findViewById(R.id.flowlayout_jkjc);
        ll_yyjl = (LinearLayout) findViewById(R.id.ll_dpatientinfo_yyjl);
        flow_yyjl = (FlowLayout) findViewById(R.id.flowlayout_yyjl);
        ll_jcbg = (LinearLayout) findViewById(R.id.ll_dpatientinfo_jcbg);
        flow_jcbg = (FlowLayout) findViewById(R.id.flowlayout_jcbg);
        ll_zkpg = (LinearLayout) findViewById(R.id.ll_dpatientinfo_zkpg);
        flow_zkpg = (FlowLayout) findViewById(R.id.flowlayout_zkpg);
        ll_dcwj = (LinearLayout) findViewById(R.id.ll_dpatientinfo_dcwj);
        flow_dcwj = (FlowLayout) findViewById(R.id.flowlayout_dcwj);
        ll_hzxx = (LinearLayout) findViewById(R.id.ll_dpatientinfo_hzxx);
        flow_hzxx = (FlowLayout) findViewById(R.id.flowlayout_hzxx);
        tv_evalution = (TextView) findViewById(R.id.tv_dpatientinfo_evalution);

        tv_name.setText(patientName);
        tv_sex.setText(patientSex);
        tv_age.setText(patientAge);
        ll_jkjc.setVisibility(View.GONE);
        ll_yyjl.setVisibility(View.GONE);
        ll_jcbg.setVisibility(View.GONE);
        ll_zkpg.setVisibility(View.GONE);
        ll_dcwj.setVisibility(View.GONE);
        ll_hzxx.setVisibility(View.GONE);

        RelativeLayout.LayoutParams lp_evalution = new RelativeLayout.LayoutParams(screenWidth / 5, screenWidth / 5);
        lp_evalution.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp_evalution.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        lp_evalution.rightMargin = ImageUtil.dip2px(10, mContext);
        lp_evalution.bottomMargin = ImageUtil.dip2px(10, mContext);
        tv_evalution.setLayoutParams(lp_evalution);
        tv_evalution.setOnClickListener(this);

        swipeRefreshLayout.setEnabled(true);
        swipeRefreshLayout.setOnRefreshListener(this);
        swipeRefreshLayout.setProgressViewOffset(true, -20, 100);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.patientUsedList(patientId, handler);
    }

    private void showData() {
        if (infoBean != null) {
            flow_jkjc.removeAllViews();
            if (infoBean.getMonitorList() != null && infoBean.getMonitorList().size() > 0) {
                ll_jkjc.setVisibility(View.VISIBLE);
                for (int i = 0; i < infoBean.getMonitorList().size(); i++) {
                    addTv(1, i, infoBean.getMonitorList().get(i).getItemName(), flow_jkjc);
                }
            } else {
//                ll_jkjc.setVisibility(View.GONE);
                ll_jkjc.setVisibility(View.VISIBLE);
                addTvnull(1, flow_jkjc);
            }

//            if (infoBean.getDrugList() != null && infoBean.getDrugList().size() > 0) {
//                ll_yyjl.setVisibility(View.VISIBLE);
//                for (int i = 0; i < infoBean.getDrugList().size(); i++) {
//                    addTv(2, i, infoBean.getDrugList().get(i).getItemName(), flow_yyjl);
//                }
//            } else {
//                ll_yyjl.setVisibility(View.GONE);
//            }
            flow_yyjl.removeAllViews();
            ll_yyjl.setVisibility(View.VISIBLE);
            if (infoBean.getDrugList() != null && infoBean.getDrugList().size() > 0) {
                addTv(2, 0, "所有记录", flow_yyjl);
                for (int i = 0; i < infoBean.getDrugList().size(); i++) {
                    addTv(2, i + 1, infoBean.getDrugList().get(i).getItemName(), flow_yyjl);
                }
            } else {
                addTvnull(2, flow_yyjl);
            }

            flow_jcbg.removeAllViews();
            if (infoBean.getAssessReportList() != null && infoBean.getAssessReportList().size() > 0) {
                ll_jcbg.setVisibility(View.VISIBLE);
                for (int i = 0; i < infoBean.getAssessReportList().size(); i++) {
                    addTv(3, i, infoBean.getAssessReportList().get(i).getItemName(), flow_jcbg);
                }
            } else {
//                ll_jcbg.setVisibility(View.GONE);
                ll_jcbg.setVisibility(View.VISIBLE);
                addTvnull(3, flow_jcbg);
            }

            flow_zkpg.removeAllViews();
            if (infoBean.getSpeciallyAssessList() != null && infoBean.getSpeciallyAssessList().size() > 0) {
                ll_zkpg.setVisibility(View.VISIBLE);
                for (int i = 0; i < infoBean.getSpeciallyAssessList().size(); i++) {
                    addTv(4, i, infoBean.getSpeciallyAssessList().get(i).getItemName(), flow_zkpg);
                }
            } else {
//                ll_zkpg.setVisibility(View.GONE);
                ll_zkpg.setVisibility(View.VISIBLE);
                addTvnull(4, flow_zkpg);
            }

            flow_dcwj.removeAllViews();
            if (infoBean.getQuestionList() != null && infoBean.getQuestionList().size() > 0) {
                ll_dcwj.setVisibility(View.VISIBLE);
                for (int i = 0; i < infoBean.getQuestionList().size(); i++) {
                    addTv(5, i, infoBean.getQuestionList().get(i).getItemName(), flow_dcwj);
                }
            } else {
//                ll_dcwj.setVisibility(View.GONE);
                ll_dcwj.setVisibility(View.VISIBLE);
                addTvnull(5, flow_dcwj);
            }

            flow_hzxx.removeAllViews();
            if (infoBean.getPatientInfoList() != null && infoBean.getPatientInfoList().size() > 0) {
                ll_hzxx.setVisibility(View.VISIBLE);
                for (int i = 0; i < infoBean.getPatientInfoList().size(); i++) {
                    addTv(6, i, infoBean.getPatientInfoList().get(i).getItemName(), flow_hzxx);
                }
            } else {
//                ll_hzxx.setVisibility(View.GONE);
                ll_hzxx.setVisibility(View.VISIBLE);
                addTvnull(6, flow_hzxx);
            }
        } else {
            toastMsg(R.string.datagetfail);
        }
    }

    private void addTv(int group, int child, String text, FlowLayout flow) {
        TextView tv = new TextView(mContext);
        ViewGroup.MarginLayoutParams lp_tv = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp5 = ImageUtil.dip2px(5, mContext);
        lp_tv.leftMargin = dp5;
        lp_tv.topMargin = dp5;
        tv.setLayoutParams(lp_tv);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        tv.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
        if (text.length() < 4) {
            tv.setPadding(ImageUtil.dip2px(12, mContext), dp5, ImageUtil.dip2px(12, mContext), dp5);
        } else {
            tv.setPadding(dp5, dp5, dp5, dp5);
        }
        tv.setBackgroundResource(R.drawable.shape_patientinfoback);

        tv.setText(text);
        tv.setId((group * 100) + child);
        tv.setOnClickListener(this);

        flow.addView(tv);
    }

    private void addTvnull(int group, FlowLayout flow) {
        TextView tv = new TextView(mContext);
        ViewGroup.MarginLayoutParams lp_tv = new ViewGroup.MarginLayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        int dp5 = ImageUtil.dip2px(5, mContext);
        lp_tv.leftMargin = dp5;
        lp_tv.topMargin = dp5;
        tv.setLayoutParams(lp_tv);
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        tv.setTextColor(ContextCompat.getColor(mContext, R.color.black_text));
        tv.setPadding(dp5, dp5, dp5, dp5);
        tv.setText(getResources().getString(R.string.havenotnow));
        flow.addView(tv);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() > 1000) {
            switch (v.getId()) {
                case R.id.iv_title_back:
                    finish();
                    break;
                case R.id.tv_dpatientinfo_evalution:
                    Intent iEvalution = new Intent(mContext, DpatientEvalutionActivity.class);
                    iEvalution.putExtra("patientid", patientId);
                    startActivity(iEvalution);
                    break;
                default:
                    break;
            }
        } else {
            int childIndex = v.getId() % 100;
            switch (v.getId() / 100) {
                case 1://健康监测
//                    String jkjc_id = infoBean.getMonitorList().get(childIndex).getItemId();
//                    Intent chartIntent = new Intent(mContext, MonitorChart1Activity.class);
//                    chartIntent.putExtra("patientId", patientId);
//                    startActivity(chartIntent);
                    Intent chartIntent = new Intent(mContext, ChartDetailActivity.class);
                    chartIntent.putExtra("patientId", patientId);
                    chartIntent.putExtra("kindId", infoBean.getMonitorList().get(childIndex).getItemId());
                    chartIntent.putExtra("name", infoBean.getMonitorList().get(childIndex).getItemName());
                    MonitorKindDao dao = new MonitorKindDao(mContext);
                    MonitorKindBean kindBean =  dao.getKind(infoBean.getMonitorList().get(childIndex).getItemId());
                    System.out.println("========" + dao.getKind(infoBean.getMonitorList().get(childIndex).getItemId()));
//                    chartIntent.putExtra("time", chartInfoList.get(position).getDate());
                    chartIntent.putExtra("min", kindBean.getMinValue());
                    chartIntent.putExtra("max", kindBean.getMaxValue());
                    if (kindBean.getValueList() != null) {
                        for (int i = 0; i < kindBean.getValueList().size(); i++) {
                            chartIntent.putExtra("text" + i, kindBean.getValueList().get(i).getName());
                        }
                    }
                    startActivity(chartIntent);
                    break;
                case 2://用药记录
                    String drugId = "";
                    if (childIndex == 0) {
                        //所有记录，没有id
                    } else {
                        childIndex--;
                        drugId = infoBean.getDrugList().get(childIndex).getItemId();
//                        toastMsg(yyjl_id);

                    }
                    Intent drugIntent = new Intent(mContext, TakeDrugsActivity.class);
                    drugIntent.putExtra("drugid", drugId);
                    drugIntent.putExtra("patientid", patientId);
                    startActivity(drugIntent);
                    break;
                case 3://检查报告
                    String jcbg_id = infoBean.getAssessReportList().get(childIndex).getItemId();
                    Intent intent = new Intent(mContext, MyReportDetail1Activity.class);
                    intent.putExtra("reportId", jcbg_id);
                    intent.putExtra("patientid", patientId);
                    intent.putExtra("reportName", infoBean.getAssessReportList().get(childIndex).getItemName());
                    intent.putExtra("reportList", (Serializable) infoBean.getAssessReportList());
                    intent.putExtra("isDoctor", true);
                    startActivity(intent);
                    break;
                case 4://专科评估
                    String zkpg_id = infoBean.getSpeciallyAssessList().get(childIndex).getItemId();
//                    toastMsg(zkpg_id);
                    Intent iZkpg = new Intent(mContext, AssessScoresActivity.class);
                    iZkpg.putExtra("patientid", patientId);
                    iZkpg.putExtra("assessid", zkpg_id);
                    iZkpg.putExtra("title", getResources().getString(R.string.speciallyassess));
                    iZkpg.putExtra("name", infoBean.getSpeciallyAssessList().get(childIndex).getItemName());
                    startActivity(iZkpg);
                    break;
                case 5://调查问卷
                    String dcwj_id = infoBean.getQuestionList().get(childIndex).getItemId();
//                    toastMsg(dcwj_id);
                    Intent iDcwj = new Intent(mContext, AssessScoresActivity.class);
                    iDcwj.putExtra("patientid", patientId);
                    iDcwj.putExtra("assessid", dcwj_id);
                    iDcwj.putExtra("title", getResources().getString(R.string.questionlist));
                    iDcwj.putExtra("name", infoBean.getQuestionList().get(childIndex).getItemName());
                    startActivity(iDcwj);
                    break;
                case 6://患者信息
                    String hzxx_id = infoBean.getPatientInfoList().get(childIndex).getItemId();
//                    toastMsg(hzxx_id);
                    Intent i_hzxx = new Intent(mContext, DpatientInfosActivity.class);
                    i_hzxx.putExtra("patientid", patientId);
                    i_hzxx.putExtra("type", hzxx_id);
                    i_hzxx.putExtra("title", infoBean.getPatientInfoList().get(childIndex).getItemName());
                    startActivity(i_hzxx);
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
